<template>
<!-- 客户管理用 渠道管理用（该渠道下的所有客户信息） -->
<!-- 客户列表页 组件 -->

  <div>
    <el-table v-if="tableData3.length>0" :data="tableData3" border style="width: 100%" >
      <el-table-column prop="CUSTOM_ID" label="客户编号" width="180">
      </el-table-column>
      <el-table-column prop="CUSTOM_NAME" label="客户名称" width="100">
      </el-table-column>
      <el-table-column prop="CUSTOM_BIRTHDATE" label="出生日期" width="180" :formatter="birthdayFomatter">
      </el-table-column>
      <el-table-column prop="CUSTOM_SCOUNTRY" label="留学国家" width="100">
      </el-table-column>
      <el-table-column prop="CUSTOM_SCHOOL" label="留学目的地国家/地区教育机构名称" width="200">
      </el-table-column>
      <el-table-column prop="CUSTOM_EMAIL" label="电子邮箱" width="200">
      </el-table-column>
      <el-table-column prop="CUSTOM_PRODUCE" label="推荐套餐" width="200">
      </el-table-column>
      <el-table-column prop="CUSTOM_LEVEL" label="客户等级" width="200">
      </el-table-column>
       <el-table-column prop="CUSTOM_ADDTIME" label="添加时间" width="200" :formatter="localDataFomatter">
      </el-table-column>      
      <el-table-column prop="ORGANIZATION_NAME" label="所属渠道" width="200">
      </el-table-column>
        <el-table-column prop="ADMIN_NAME" label="客户负责人" width="200">
      </el-table-column>
      <el-table-column label="操作" width="250"  fixed="right" v-if="type!='渠道'">
        <template scope="scope">
          <el-button type="text" size="media" @click="goAddCommunication(scope.row.CUSTOM_ID)">
            <i class="el-icon-view"> 沟通</i>
          </el-button>
          <el-button type="text" size="media" v-if="type=='待续保'">
            <i class="el-icon-view"> 续保</i>
          </el-button>    
            <el-button type="text" size="media"  @click="goEditer(scope.row.CUSTOM_ID)">
            <i class="el-icon-view"> 查看</i>
          </el-button>  
           <el-button type="text" size="media" v-if="scope.row.CUSTOM_ISSEALD!='已购买'"  @click="goSeald(scope.row)">
            <i class="el-icon-view"> 购买</i>
          </el-button>          
        </template>
      </el-table-column>
    
    </el-table>
    <div class="block">
      <el-pagination @current-change="handleCurrentChange" :current-page="currentPage1" :page-size="10" layout="total, prev, pager, next" :total="pagecount">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import getid from "@/js/getid";
import api from "@/js/api";
export default {
  data() {
    return {
      pagecount: 0,
      currentPage1: 1,
      tableData3: [],
      Sdate: {
        name: "",
        school: "",
        produce: "",
        orgnization: "",
        time: []
      }
    };
  },
  props: {
    id: {
      type: {
        type: String,
        default: ""
      }
    },
    type: {
      type: String,
      default: ""
    },
    //Organzation id
    OrganzationId: {
      type: String,
      default: ""
    }
  },
  methods: {
    localDataFomatter(row, column, cellValue) {
      if(cellValue){
      cellValue = cellValue + "Z";
      var data = new Date(cellValue);
      var dataString =
        data.getFullYear() +
        "/" +
        (data.getMonth() + 1 >= 10
          ? data.getMonth() + 1
          : "0" + (data.getMonth() + 1)) +
        "/" +
        (data.getDate() >= 10 ? data.getDate() : "0" + data.getDate()) +
        " " +
        (data.getHours() >= 10 ? data.getHours() : "0" + data.getHours()) +
        ":" +
        (data.getMinutes() >= 10 ? data.getMinutes() : "0" + data.getMinutes());
      return dataString;
      }
    },
    birthdayFomatter(row, column, cellValue) {
      cellValue = cellValue + "Z";
      return new Date(cellValue).toLocaleDateString();
    },
    dataFomatter(row, column, cellValue) {
      cellValue = cellValue + "Z";
      return new Date(cellValue).toLocaleString();
    },
    //添加沟通记录
    goAddCommunication(cid) {
      let data = {
        id: cid,
        type: "客户"
      };
      this.$emit("goAddCommunication", data);
    },
    //更换页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.Mselect(this.Sdate, true);
    },
    //编辑
    goEditer(id) {
      this.$emit("goCustomEditer", id);
    },
    //修改数据源数据
    customgetSelectDate() {
      // let that = this;
      // for (let i in this.tableData3) {
      //   console.log(that.tableData3[i]);
      //   if (that.tableData3[i].ORGANIZATION.length > 0) {
      //      that.tableData3[i].ORGANIZATION_NAME =
      //       that.tableData3[i].ORGANIZATION[0].ORGANIZATION_NAME;
      //   }
      // }
    },
    //
    goSeald(model) {
      // console.log(model);
      console.log(JSON.stringify(model));
      sessionStorage.setItem("custom",JSON.stringify(model));
      this.$router.push("Selladd");
    },
    //多条件查询
    Mselect(Mdate, currentChange) {
      // console.log(12);
      this.Sdate = Mdate;
      // console.log(Mdate);
      let that = this;
      var data = {
        name: Mdate.name == "" ? null : Mdate.name,
        school: Mdate.school == "" ? null : Mdate.school,
        produce: Mdate.produce == "" ? null : Mdate.produce,
        orgnization: Mdate.orgnization == "" ? null : Mdate.orgnization
      };
      if (Mdate.time.length > 0) {
        data.startTime = getid.getDateString(Mdate.time[0]);
        data.endTime = getid.getDateString(Mdate.time[1]);
      } else {
        data.startTime = null;
        data.endTime = null;
      }
      if (!currentChange) {
        this.currentPage = 1;
      }
      data.pageIndex = this.currentPage;
      data.pageSize = 10;
      // console.log(33);
      var role = sessionStorage.getItem("userRole");
      if (role == "销售助理" || role == "销售主管") {
        data.userId = null;
      } else {
        data.userId = sessionStorage.getItem("userId");
      }
      // console.log(22);
      api.CustomGetMutial(that, data, function(msg) {
        that.tableData3 = msg.Result;
        that.pagecount = msg.ListCount;
        that.customgetSelectDate();
      });
    },
    //获取数据
    Getdata() {
      var role = sessionStorage.getItem("userRole");
      if (role == "销售助理" || role == "销售主管") {
        var data = {
          pageIndex: this.currentPage,
          pageSize: 10,
          userId: sessionStorage.getItem("userId")
        };

        if (this.type == "待沟通") {
          let that = this;
          api.CustomGetRemainList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
            that.customgetSelectDate();
          });
        } else {
          let that = this;
          api.CustomGetList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
            that.customgetSelectDate();
          });
        }
      } else {
        // 销售员
        var data = {
          pageIndex: this.currentPage,
          pageSize: 10,
          userId: sessionStorage.getItem("userId")
        };
        if (this.type == "待沟通") {
          let that = this;
          api.CustomGetRemainListByUser(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
            that.customgetSelectDate();
          });
        } else {
          let that = this;
          api.CustomGetUserList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
            that.customgetSelectDate();
          });
        }
      }
      this.customgetSelectDate();
    }
  },
  created() {
    this.currentPage = 1;
    if (this.type != "渠道") {
      this.Getdata();
    } else {
      // console.log(this.OrganzationId);
      //如果是渠道 进行展示的客户信息 根据 传来的客户id进行查询
      var data = {
        id: this.OrganzationId,
        pageIndex: this.currentPage,
        pageSize: 10
      };
      // console.log(data);
      let that = this;

      api.CustomGetByOrganizationId(that, data, function(msg) {
        that.tableData3 = msg.Result;
        that.pagecount = msg.ListCount;
        that.customgetSelectDate();
      });
    }
  }
};
</script>
